package com.showsu.editor.utils.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.showsu.editor.entity.dto.*;
import com.showsu.editor.entity.vo.XhsAwemeDetail;
import com.showsu.editor.entity.vo.response.VnilVO;
import com.showsu.editor.enums.ModerationRiskLevelEnum;
import com.showsu.editor.enums.ResultCode;
import com.showsu.editor.utils.BsResult;
import com.showsu.editor.utils.Constant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: jph
 * @Description: 数秀工具栏服务：
 * @Date: Created in 15:01 下午 2023/11/10
 * @Modified By:
 */
@Slf4j
@Component
public class ShuXiuUtils {

    @Value("${shuxiu.utils.url}")
    private String utilsUrl;

    @Value("${shuxiu.utils.authorization}")
    private String authorization;

    /**
     * 小红书获取作品详情
     */
    public ShuXiuCommonDTO getXhsAwemeDetail(XhsPortalParseDTO req){
        ShuXiuCommonDTO result=null;
        try {;
            String url=utilsUrl + "/api/utils/getXhsAwemeDetail";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .body(JSON.toJSONString(req))
                    .timeout(Constant.defaultTimeOut).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("getXhsAwemeDetail:{}",e);}
        return result;
    }

    /**
     * 小红书用户笔记限流结果
     */
    public ShuXiuCommonDTO getXhsAwemeLimited(CheckAwemeIncludedDTO req) {
        ShuXiuCommonDTO result = null;
        try {
            ;
            String url = utilsUrl + "/api/utils/getXhsAwemeLimited";
            String response = HttpUtil.createPost(url)
                    .header("authorization", authorization)
                    .body(JSON.toJSONString(req))
                    .timeout(Constant.defaultTimeOut).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result = JSON.parseObject(response, ShuXiuCommonDTO.class);
            }
        } catch (Exception e) {
            log.error("getXhsAwemeDetail:{}", e);
        }
        return result;
    }

    /**
     * 小红书-笔记收录查询-搜素结果
     */
    public ShuXiuCommonDTO getAwemeIncludedSearchResult(CheckAwemeIncludedDTO req){
        ShuXiuCommonDTO result=null;
        try {;
            String url=utilsUrl + "/api/utils/getAwemeIncludedSearch";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .body(JSON.toJSONString(req))
                    .timeout(Constant.defaultTimeOut).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("getAwemeIncludedSearchResult:{}",e);}
        return result;
    }

    /**
     * 小红书-笔记收录查询-收录结果
     */
    public ShuXiuCommonDTO getAwemeIncluded(CheckAwemeIncludedDTO req){
        ShuXiuCommonDTO result=null;
        try {;
            String url=utilsUrl + "/api/utils/getAwemeIncluded";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .body(JSON.toJSONString(req))
                    .timeout(Constant.defaultTimeOut).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("getXhsAwemeDetail:{}",e);}
        return result;
    }


    /**
     * 小红书分享开放签名生成
     */
    public ShuXiuCommonDTO xhsAgoraSignature(){
        ShuXiuCommonDTO result=null;
        try {;
            String url=utilsUrl + "/api/utils/xhsAgora/signature";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .timeout(Constant.defaultTimeOut).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("xhsAgoraSignature:{}",e);}
        return result;
    }

    /**
     * 微信小程序内容安全检测工具
     * @throws IOException
     */
    public ModerationRiskLevelEnum wxMaSecCheck(Integer scene, String text){
        try {
            WxMaSecCheckDTO req=new WxMaSecCheckDTO();
            req.setScene(scene);
            req.setText(text);
            String url=utilsUrl + "/api/utils/wxMaSecCheck?scene=" + scene;
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isNotBlank(response)) {
                JSONObject res=JSON.parseObject(response);
                if(res!=null&&res.getString(BsResult.CODE_KEY).equals("200")){
                    String code=ModerationRiskLevelEnum.unknown.getCode();
                    try {
                        code=res.getString(BsResult.DATA_KEY);
                    }catch (Exception e){
                        log.error("wxMaSecCheck:{}",e);
                    }
                    return ModerationRiskLevelEnum.getByCode(code);
                }

            }
        }catch (Exception e){log.error("wxMaSecCheck:{}",e);}
        return ModerationRiskLevelEnum.unknown;
    }

    /**
     * 去水印
     * @throws IOException
     */
    public ShuXiuCommonDTO dewater(String source){
        ShuXiuCommonDTO result=null;
        try {
            XhsDewaterDTO req=new XhsDewaterDTO();
            req.setSource(source);
            String url=utilsUrl + "/api/utils/dewater";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("dewater:{}",e);}
        return result;
    }


    /**
     * 从作品评论中提取评论、图片信息
     * @throws IOException
     */
    public ShuXiuCommonDTO commentInfoDewater(String source){
        ShuXiuCommonDTO result=null;
        try {
            XhsDewaterDTO req=new XhsDewaterDTO();
            req.setSource(source);
            String url=utilsUrl + "/api/utils/dewater/commentInfo";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("dewater:{}",e);}
        return result;
    }

    /**
     * 去水印
     * @throws IOException
     */
    public ShuXiuCommonDTO weiboLiveDewater(String source){
        ShuXiuCommonDTO result=null;
        try {
            XhsDewaterDTO req=new XhsDewaterDTO();
            req.setSource(source);
            String url=utilsUrl + "/api/utils/dewater/weibo/live";
            String response = HttpUtil.createPost(url)
                    .header("authorization",authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isNotBlank(response)) {
                result=JSON.parseObject(response,ShuXiuCommonDTO.class);

            }
        }catch (Exception e){log.error("weiboLiveDewater:{}",e);}
        return result;
    }

    /**
     * 获取xhs笔记推荐关键词
     * @param awemeId
     * @return
     */
    public List<XhsSearchHotKeywordDTO> getXhsAwemeRecommondKeywords(String awemeId) {
        List<XhsSearchHotKeywordDTO> result = new ArrayList<>();
        try {
            XhsDewaterDTO req = new XhsDewaterDTO();
            req.setAwemeId(awemeId);
            String url = utilsUrl + "/api/utils/getXhsAwemeRecommondKeywords";
            String response = HttpUtil.createPost(url)
                    .header("authorization", authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isBlank(response)) {
                return result;
            }
            ShuXiuCommonDTO shuXiuCommonDTO = JSON.parseObject(response, ShuXiuCommonDTO.class);
            if (shuXiuCommonDTO != null
                    && shuXiuCommonDTO.getCode().equals(String.valueOf(ResultCode.SUCCESS.getCode()))
                    && StringUtils.isNotBlank(shuXiuCommonDTO.getData())) {
                result = JSON.parseArray(shuXiuCommonDTO.getData(), XhsSearchHotKeywordDTO.class);
            }
        } catch (Exception e) {
            log.error("getXhsAwemeRecommondKeywords error:{}", e);
        }
        return result;
    }

    /**
     * 获取xhs关键词，以词推词
     * @param keyword
     * @return
     */
    public List<XhsSearchHotKeywordDTO> getXhsHotKeywords(String keyword) {
        List<XhsSearchHotKeywordDTO> result = new ArrayList<>();
        try {
            XhsDewaterDTO req = new XhsDewaterDTO();
            req.setKeyword(keyword);
            String url = utilsUrl + "/api/utils/getXhsHotKeywords";
            String response = HttpUtil.createPost(url)
                    .header("authorization", authorization)
                    .timeout(Constant.defaultTimeOut).body(JSON.toJSONString(req)).execute().body();
            if (StringUtils.isBlank(response)) {
                return result;
            }
            ShuXiuCommonDTO shuXiuCommonDTO = JSON.parseObject(response, ShuXiuCommonDTO.class);
            if (shuXiuCommonDTO != null
                    && shuXiuCommonDTO.getCode().equals(String.valueOf(ResultCode.SUCCESS.getCode()))
                    && StringUtils.isNotBlank(shuXiuCommonDTO.getData())) {
                result = JSON.parseArray(shuXiuCommonDTO.getData(), XhsSearchHotKeywordDTO.class);
            }
        } catch (Exception e) {
            log.error("getXhsAwemeRecommondKeywords error:{}", e);
        }
        return result;
    }

    public static void main(String[] args) {
        ShuXiuUtils util=new ShuXiuUtils();
        log.error(util.wxMaSecCheck(2," 互相裸露 互裸朋友 裸聊").getMsg());

    }


}
